package fucai.com.fucai.blmanager;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.util.List;

import fucai.com.fucai.R;
import fucai.com.fucai.db.FuCaiNumber;
import fucai.com.fucai.utils.AppUtils;


/**
 * all business layer DataManager's parent
 *
 * @author nicky.han
 */
public class BaseBLManager {
    private SharedPreferences mPerference;
    private SharedPreferences.Editor mEditor;
    public List<FuCaiNumber> mFuCaiNumbers = null;
    /**
     * Count about load items per page
     */
    public static final int DEFAULT_PER_PAGE_COUNT = 20;
    /**
     * Used to get UI resource
     */
    public Context mContext = null;
    /**
     * Use to operate the local DB
     */
    /*private static DatabaseManager mDBManager = null;
*/

    /**
     * Initialize adapter
     *
     * @param context
     */
    public BaseBLManager(Context context) {
        mContext = context;
    }

    /**
     * Open the DatabaseManager before operate DB
     */
   /* public DatabaseManager getDBManager()
    {
        if (mDBManager == null)
        {
            mDBManager = new DatabaseManager(mContext);
        }
        return mDBManager;
    }*/

    /**
     * close the DatabaseManager when it is useless
     */
 /*   public void closeDBManager()
    {
        if (null != mDBManager)
        {
            mDBManager.closeManager();
            mDBManager = null;
        }
    }*/


    /**
     * used to check the network is available or not
     *
     * @return
     */
    public static boolean checkNetwork(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Show toast using the message and duration.
     *
     * @param message  the message shown in toast.
     * @param duration the time for which the toast will be shown.
     */

    public void showToastMessage(String message, int duration) {
        AppUtils.showToastMessage(mContext, message, duration);
    }


    /**
     * 保存用户基本信息
     *
     * @param info 用户信息的数组
     * @return 保存结果
     */
    public boolean saveUserLoginBasicInfo(String[] info) {
        mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
        mEditor = mPerference.edit();
        mEditor.putString(mContext.getString(R.string.isfrist), info[0]);
        return mEditor.commit();
    }

    public boolean checkIsFrist() {
        mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
        String isFrist = mPerference.getString(mContext.getString(R.string.isfrist), mContext.getString(R.string.isflase));
        if ((mContext.getString(R.string.istrue)).equals(isFrist)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 保存上一次更新的期数
     *
     * @param issue
     */
    public void saveIssue(String issue) {
        mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
        mEditor = mPerference.edit();
        mEditor.putString(mContext.getString(R.string.issue), issue);
        mEditor.commit();
    }

    /**
     * 得到更新的期数
     * @return
     */
    public String getIssue() {
        mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.app_name), Context.MODE_PRIVATE);
        return mPerference.getString(mContext.getString(R.string.isfrist), "-1");  //默认最新的
    }
    /**
     * 从本地保存中获取用户基本信息
     * @return 用户信息数组
     *//*
    public String[] getUserInfo()
    {
        String[] loginInfo = new String[3];
        SharedPreferences mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        loginInfo[0] = mPerference.getString(Constant.USER_NAME, "");
        loginInfo[1] = decriptPassword(mPerference.getString(Constant.USER_PASSWORD, ""));
        loginInfo[2] = mPerference.getString(Constant.REMEMBER_PASSWORD, "");
        return loginInfo;
    }

    *//**
     * 获取用户类型
     * @return
     *//*
    public String getUserType()
    {
        SharedPreferences mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        return mPerference.getString(Constant.USER_TYPE, "");
    }

    *//**
     * 获取用户ID
     * @return
     *//*
    public String getUserID()
    {
        SharedPreferences mPerference = mContext.getSharedPreferences(mContext.getResources().getString(R.string.preference_key), Context.MODE_PRIVATE);
        return mPerference.getString(Constant.USER_ID, "");
    }*/
}
